package com.hmsm.sys.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import com.hmsm.sys.action.OrgTreeHelper;
import com.hmsm.sys.bean.LocalUser;
import com.hmsm.sys.bean.RTXDeptBean;

@Controller
public class OrgController {
	
	private Log log = LogFactory.getLog(OrgController.class);
	
	public final static String JSON = "json";
		
	@Resource
	private OrgTreeHelper orgTreeHelper;

	@SuppressWarnings("rawtypes")
	public ModelAndView manage(LocalUser currentUser, Map<String, Object> session) {
		ModelAndView modelAndView = new ModelAndView("success");
		
		RTXDeptBean rootDept = orgTreeHelper.getRootDept(orgTreeHelper.getOrgManageRight(), true, currentUser);
		session.put(OrgTreeHelper.ROOT_KEY, rootDept);
		
		try {
		} catch (Exception e) {
			String errorMessage = e.getMessage();
			modelAndView.getModel().put("errorMessage", errorMessage);
			modelAndView.setViewName("error");;
		}
		
		return modelAndView;
	}

	@SuppressWarnings("rawtypes")
	public ModelAndView sync(List<Integer> addDeptIds, List<Integer> updDeptIds, List<Integer> updUserIds, List<Integer> addUserIds, List<Integer> delUserIds, List<Integer> delDeptIds, LocalUser currentUser, Map<String, Object> session) {
		ModelAndView modelAndView = new ModelAndView(JSON);
		
		String message = "";
		String errorMessage;
		try {
			RTXDeptBean rootDept = (RTXDeptBean)session.get(OrgTreeHelper.ROOT_KEY);
			if (rootDept==null){
				return errorReturn(modelAndView, "组织机构树未初始化！");
			}else{
				if(rootDept.getBindDept().getUserLoginNameMap()==null){
					return errorReturn(modelAndView, "组织机构树未初始化异常！");
				}
			}
			orgTreeHelper.setRootDept(rootDept, currentUser);
			log.debug("开始同步！");
			
			if(addDeptIds.size()>0){
				int addDeptCount = orgTreeHelper.addDepts(addDeptIds, currentUser);
				if (addDeptCount>0){
					modelAndView.getModel().put("addDeptCount", addDeptCount);
					message += "添加了"+addDeptCount+"个部门，";
				}
			}
			if(updDeptIds.size()>0){
				int count = orgTreeHelper.updateDepts(updDeptIds, currentUser);
				if (count>0){
					modelAndView.getModel().put("updDeptCount", count);
					message += "更新了"+count+"个部门，";
				}
			}
			if(updUserIds.size()>0){
				int count = orgTreeHelper.updateUsers(updUserIds, currentUser);
				if (count>0){
					modelAndView.getModel().put("updUserCount", count);
					message += "更新了"+count+"个用户，";
				}
			}
			if(addUserIds.size()>0){
				int addUserCount = orgTreeHelper.addUsers(addUserIds, currentUser);
				if (addUserCount>0){
					modelAndView.getModel().put("addUsetCount", addUserCount);
					message += "添加了"+addUserCount+"个用户，";
				}
			}
			if(delUserIds.size()>0){
				Map<String, Object> result = orgTreeHelper.delUsers(delUserIds, currentUser);
				int delUserCount = (Integer)result.get("count");
				if (delUserCount>0){
					modelAndView.getModel().put("delUserCount", delUserCount);
					message += "删除了"+delUserCount+"个用户，";
				}
				int errorCount = (Integer)result.get("errorCount");
				errorMessage = (String)result.get("errorMessage");
				if(errorCount>0){
					message += "其中"+errorCount+"个用户删除失败，原因是："+errorMessage;
				}
			}
			if(delDeptIds.size()>0){
				int delDeptCount = orgTreeHelper.delDepts(delDeptIds, currentUser);
				if (delDeptCount>0){
					modelAndView.getModel().put("delDeptCount", delDeptCount);
					message += "删除了"+delDeptCount+"个部门，";
				}
			}
			log.debug(message);

			modelAndView.getModel().put("success", true);
			modelAndView.getModel().put("message", message);
		} catch (Exception e) {
			e.printStackTrace();
			modelAndView.getModel().put("success", false);
			modelAndView.getModel().put("errorMessage", ""+e.getMessage());
		}
		
		return modelAndView;
	}

	private ModelAndView errorReturn(ModelAndView modelAndView, String errorMessage) {
		modelAndView.setViewName("error");
		modelAndView.getModel().put("errorMessage", errorMessage);
		return modelAndView;
	}

}
